package com.example.basic.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * @author mjw
 */
@Data
public class ReadListener<T> extends AnalysisEventListener<T> {

    public static final Logger LOGGER = LoggerFactory.getLogger(ReadListener.class);

    /**
     * 自定义用于暂时存储data
     */
    private List<T> dataList = new ArrayList<>();


    /**
     * 这个每一条数据解析都会来调用
     */
    @Override
    public void invoke(T data, AnalysisContext context) {
        System.out.println(data);
        dataList.add(data);
//        String headStr = JSON.toJSONString(data);
//        dataList.add(JSONObject.parseObject(headStr));
    }

    /**
     * 所有数据解析完成了 都会来调用
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        LOGGER.info("Excel解析完毕");
    }

}